package com.xiao.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.common.Constants;
import com.xiao.common.Result;
import com.xiao.common.dto.UserDto;
import com.xiao.dao.UserIDMappe;
import com.xiao.pojo.Admin;
import com.xiao.pojo.Userid;
import com.xiao.server.RegisterServer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegisterServerImpl implements RegisterServer {
   @Autowired
    private UserIDMappe userIDMappe;

    @Override
    public Result registerUser(UserDto userDto) {

        try {
            QueryWrapper<Userid> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_ID", userDto.getUserId());
            List<Userid> list = userIDMappe.selectList(queryWrapper);
            if (list.size() != 0){
                return Result.error(Constants.CODE_500,"用户名已存在!");
            }else{
                Userid userid = new Userid();
                BeanUtils.copyProperties(userDto, userid );
                if(userIDMappe.insert(userid) == 1){
                    return Result.success();
                }
               return Result.error();
            }
        } catch (Exception e) {
            return Result.error();
        }
    }
}
